<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/4/8
 * Time: 9:40
 * 教师管理
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class Teacher extends Base {
    public function index(){
        if(request()->isAjax ()){
            $params = input('param.');
            //日期
            if(isset($params['add_start']) && $params['add_start'] != '' && isset($params['add_end']) && $params['add_end'] != ''){
                $where['user_time'] = ['between', [$params['add_start'], $params['add_end']]];
            }
            if(isset($params['add_start']) && $params['add_start'] != '' && isset($params['add_end']) && $params['add_end'] == ''){
                $where['user_time'] = ['>=', $params['add_start']];
            }if(isset($params['add_start']) && $params['add_start'] == '' && isset($params['add_end']) && $params['add_end'] != ''){
                $where['user_time'] = ['<=', $params['add_end']];
            }
            //关键字
            if(isset($params['keyword']) && $params['keyword'] != ''){
                $keyword = $params['keyword'];
                $where['a.user_id|a.user_phone'] = array('like',"%$keyword%");
            }
            if(isset($params['province']) && $params['province'] != ''){
                $where['a.user_sheng'] =$params['province'] ;
            }
            if(isset($params['city']) && $params['city'] != ''){
                $where['a.user_shi'] = $params['city'];
            }
            if(isset($params['district']) && $params['district'] != ''){
                $where['a.user_qu'] = $params['district'];
            }
            if(isset($params['user_school']) && $params['user_school'] != ''){
                $where['a.user_school'] = $params['user_school'];
            }
            //状态
            if(isset($params['user_status']) && $params['user_status'] != ''){
                $where['user_status'] = $params['user_status'];
            }
            $where['a.user_type'] = 2;
            $where['a.user_status'] =array('IN',array(1,5,2,6));
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('u_user')->alias('a')
                ->join('up_area ua','ua.district_id = a.user_sheng','left')
                ->join('up_area ua1','ua1.district_id = a.user_sheng','left')
                ->join('up_area ua2','ua2.district_id = a.user_sheng','left')
                ->join('up_school us','us.school_id = a.user_school','left')
                ->where($where)->count();// 获取总条数
            $count2 = Db::name('u_user')->alias('a')
                ->join('up_area ua','ua.district_id = a.user_sheng','left')
                ->join('up_area ua1','ua1.district_id = a.user_sheng','left')
                ->join('up_area ua2','ua2.district_id = a.user_sheng','left')
                ->join('up_school us','us.school_id = a.user_school','left')
                ->where($where)
                ->column('user_id');// 获取总条数
            $lists = Db::name('u_user')->alias('a')
                ->join('up_area ua','ua.district_id = a.user_sheng','left')
                ->join('up_area ua1','ua1.district_id = a.user_shi','left')
                ->join('up_area ua2','ua2.district_id = a.user_qu','left')
                ->join('up_school us','us.school_id = a.user_school','left')
                ->where($where)
                ->field('a.user_status,a.user_gender,a.user_id,us.school_name,a.user_time,
                a.user_id,a.user_username,a.user_gender,a.user_phone,
                ua.district,ua1.district as district1,ua2.district as district2,user_card_front,
                user_card_back,user_zige')
                ->page($Nowpage,$limits)
                ->order('user_time desc')
                ->select();
            foreach ($lists as $key=>&$vo){
                $aa = array();
                if($vo['user_status']==5){
                    $vo['status'] = '审核中';
                }
                if($vo['user_status']==2){
                    $vo['status'] = '拒绝';
                }
                if($vo['user_status']==6){
                    $vo['status'] = '未提交审核';
                }
                if($vo['user_status']==1){
                    $vo['status'] = '正常';
                }
                if($vo['user_gender']==1){
                    $vo['gender'] = '男';
                }
                if($vo['user_gender']==2){
                    $vo['gender'] = '女';
                }
                $vo['address'] = $vo['district'].$vo['district1'].$vo['district2'];
                $info = Db::table('up_grade_class')->alias('a')
                         ->join('up_grade ug','ug.grade_id = a.gs_grade_id','left')
                         ->join('up_class uc','uc.class_id = a.gs_class_id','left')
                         ->field('ug.grade_name,uc.class_name')
                         ->where('a.gs_user_id',$vo['user_id'])
                         ->select();
                 foreach ($info as $key1=>&$vo1){
                     $aa[] = $vo1['grade_name'].'-'.$vo1['class_name'];
                 }
                $vo['info'] = implode(',',$aa);
            }
            $str = '';
            if($count2){
                $str = implode($count2,',');
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'count2' => $str, 'data' => $lists]);
        }
        $where1['school_status']  = 1;
        $where1['school_del']  = 1;
        $info1 = Db::table('up_school')->where($where1)->select();
        $province = Db::table('up_area')->where('level', 1)->select();
        $this->assign('province', $province);
        $this->assign('info1',$info1);
        return $this->fetch();
    }

    /***
     * 批量通过
     */
    public function usingAllS(){
        $ids = input('ids');
        $num = input('num');
        if($ids == ''){
            return json(['code' => 100, 'msg' => '请选择教师']);
        }
        $status = Db::table('up_u_user')->where('user_id', 'IN', $ids)->column('user_status');
        if($num == 1){//上架
            if(in_array(2, $status)){
                return json(['code' => 100, 'msg' => '所选教师中含有已拒绝，请重新选择']);
            }
        }
        $res = Db::table('up_u_user')->where('user_id', 'IN', $ids)->setField('user_status', $num);
        if($res){
            return json(['code' => 200, 'msg' => "操作成功"]);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }

    }
    /***
     * 批量拒绝
     */
    public function batchForbiddenS(){
        $ids = input('ids');
        $num = input('num');
        if($ids == ''){
            return json(['code' => 100, 'msg' => '请选择教师']);
        }
        $status = Db::table('up_u_user')->where('user_id', 'IN', $ids)->column('user_status');
        if($num == 2){//上架
            if(in_array(1, $status)){
                return json(['code' => 100, 'msg' => '所选教师中含有已通过的，请重新选择']);
            }
        }
        $res = Db::table('up_u_user')->where('user_id', 'IN', $ids)->setField('user_status', $num);
        if($res){
            return json(['code' => 200, 'msg' => "操作成功"]);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    /***
     * 批量导出
     */
    public function excelIn(){
        $ids = input('param.ids');
        if($ids == ''){
            return json(['code' => 100, 'msg' => '请选择教师']);
        }
        $where['a.user_id'] = array('IN',$ids);
        $lists = Db::name('u_user')->alias('a')
            ->join('up_area ua','ua.district_id = a.user_sheng','left')
            ->join('up_area ua1','ua1.district_id = a.user_shi','left')
            ->join('up_area ua2','ua2.district_id = a.user_qu','left')
            ->join('up_school us','us.school_id = a.user_school','left')
            ->where($where)
            ->field('a.user_status,a.user_gender,a.user_nickname,a.user_id,us.school_name,a.user_time,
                a.user_id,a.user_username,
                ua.district,ua1.district as district1,ua2.district as district2')
            ->select();
        $aa = array();
        foreach ($lists as $key=>&$vo){
            if($vo['user_status']==5){
                $vo['status'] = '审核中';
            }elseif ($vo['user_status']==2){
                $vo['status'] = '拒绝';
            }else{
                $vo['status'] = '';
            }
            if($vo['user_gender']==1){
                $vo['gender'] = '男';
            }elseif ($vo['user_gender']==2){
                $vo['gender'] = '女';
            }else{
                $vo['gender'] = '';
            }
            $vo['address'] = $vo['district'].$vo['district1'].$vo['district2'];
            $info = Db::table('up_grade_class')->alias('a')
                ->join('up_grade ug','ug.grade_id = a.gs_grade_id','left')
                ->join('up_class uc','uc.class_id = a.gs_class_id','left')
                ->field('ug.grade_name,uc.class_name')
                ->where('a.gs_user_id',$vo['user_id'])
                ->select();
            foreach ($info as $key1=>&$vo1){
                $aa[] = $vo1['grade_name'].'-'.$vo1['class_name'];
            }
            $vo['info'] = implode(',',$aa);
            unset($lists[$key]['user_status']);
            unset($lists[$key]['user_gender']);
            unset($lists[$key]['district']);
            unset($lists[$key]['district1']);
            unset($lists[$key]['district2']);
        }
        $cellname = [
            ['user_nickname','昵称',20,'LEFT'],
            ['user_id','用户ID',15,'LEFT'],
            ['school_name','认证学校',15,'LEFT'],
            ['user_time','提交时间',15,'LEFT'],
            ['user_username','姓名',15,'LEFT'],
            ['status','审核状态',15,'LEFT'],
            ['gender','性别',15,'LEFT'],
            ['address','所在地',15,'LEFT'],
            ['info','认证年级-班级',30,'LEFT'],
        ];
        $res = exportExcel('教师审核管理列表','Teacher',$cellname,$lists);
        return json($res);
    }

    /***
     * @return mixed
     * 详情
     */
    public function detail(){
        $id = input('param.id');
        $info = Db::table('up_u_user')->where('user_id',$id)->field('user_card_front')->find();
        $info['res'] = explode(',',$info['user_card_front']);
        $this->assign('info',$info);
        return $this->fetch();
    }
}